//app.js
var util = require("./utils/utils")
App({
      data: {},
      //全局数据
      globalData: {
        //用户信息
        userInfo: null,
      },
     
        onLaunch: function () {
          if (!wx.cloud) {
            console.error('请使用 2.2.3 或以上的基础库以使用云能力')
          } else {
            wx.cloud.init({
              env: 'meeting2021-2glaokedaf06ae2e',
              traceUser: true,
            })
          }

          this.globalData = {}

          //从缓存中获取用户数据
          const userInfo = wx.getStorageSync("userInfo")
          if (userInfo) {
            this.globalData.userInfo = userInfo
          }

        },


        /* 拿到时间进行判断是否进行更新 */
        //假设时间已经判断，现在进行更新数据库操作
        onShow: function () {

          //准备要更新的初始数据
          let time = new Date(),
            year = time.getFullYear(),
            month = time.getMonth() + 1,
            day = time.getDate(),
            hour = time.getHours(),
            minutes = time.getMinutes() / 60,
            today = year + '-' + month + '-' + day;

          wx.cloud.database().collection("meetingRoom").get({

            success: res => {
              let roomList = res.data;


              for (let i = 0; i < roomList.length; i++) {
                let timeList = roomList[i].timeList;

                //监视日期状态，对日期进行更新
                if (!(timeList[0].specDate === today)) { //需要更新

                  console.log("需要更新")

                  if (timeList[1].specDate === today) { //是创建数据库的第二天

                    console.log("这是创建数据库的第二天！")

                    let day2 = day + 2;
                    let newTimeObj = {
                      'specDate': year + "-" + month + "-" + day2,
                      'openTime': timeList[2].openTime,
                    }

                    //组合新时间对象
                    timeList.shift();
                    timeList.push(newTimeObj);

                    wx.cloud.database().collection('meetingRoom').doc(roomList[i]._id).update({
                      data: {
                        timeList: timeList
                      },
                      success: res => {
                        console.log("更新成功！")
                      }
                    })
                  } else if (timeList[2].specDate === today) { //是创建数据库的第三天

                    console.log("这是创建数据库的第三天！")

                    let day1 = day + 1;
                    let day2 = day + 2;
                    let newTimeObj1 = {
                      'specDate': year + "-" + month + "-" + day1,
                      'openTime': timeList[2].openTime,
                    }
                    let newTimeObj2 = {
                      'specDate': year + "-" + month + "-" + day2,
                      'openTime': timeList[2].openTime,
                    }

                    timeList.shift();
                    timeList.shift();
                    timeList.push(newTimeObj1);
                    timeList.push(newTimeObj2);

                    wx.cloud.database().collection('meetingRoom').doc(roomList[i]._id).update({
                      data: {
                        timeList: timeList
                      },
                      success: res => {
                        console.log("更新成功！")
                      }
                    })


                  } else { //过了三天, 时间全部更新, 以数据库中的最后一天为准

                    console.log("这是创建数据库过了三天！")
                    let day1 = day + 1;
                    let day2 = day + 2;
                    let newTimeObj1 = {
                      'specDate': year + "-" + month + "-" + day,
                      'openTime': timeList[2].openTime,
                    }
                    let newTimeObj2 = {
                      'specDate': year + "-" + month + "-" + day1,
                      'openTime': timeList[2].openTime,
                    }
                    let newTimeObj3 = {
                      'specDate': year + "-" + month + "-" + day2,
                      'openTime': timeList[2].openTime,
                    }

                    timeList = [];
                    timeList.push(newTimeObj1);
                    timeList.push(newTimeObj2);
                    timeList.push(newTimeObj3);

                    wx.cloud.database().collection('meetingRoom').doc(roomList[i]._id).update({
                      data: {
                        timeList: timeList
                      },
                      success: res => {
                        console.log("更新成功！")
                      }
                    })

                  }
                }

                //监视时间状态，过期时间不再重新预定,更改标志位
                // 08:00, 10:00, 14:00, 16:00, 19:00, 21:00
                let nowTime = hour + minutes;
                let time = roomList[i].timeList[0].openTime;
                for (let index in time) {
                  let meetingHour = Number(index.substr(0, 2));
                  if (meetingHour <= nowTime) { //已经过时，需要更改

                    if (time[index].state === 1) {
                      time[index].state = 4
                    }

                  }
                }
                timeList[0].openTime = time;

                //数据库更新
                wx.cloud.database().collection('meetingRoom').doc(roomList[i]._id).update({
                  data: {
                    timeList: timeList
                  },
                  success: res => {
                    console.log("更新成功！")
                  }
                })

              }
            }
          })
        }
      })